package com.sakura.util.redis;

import com.sakura.ZeroApplication;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.concurrent.TimeUnit;

/**
 * Created by apple on 2018/6/26.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:*.xml"})
@SpringBootTest(classes = ZeroApplication.class)
@Slf4j
public class ApplicationTests {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisDAO redisDAO;

    @Test
    public void test() throws Exception {

        // 保存字符串
        stringRedisTemplate.opsForValue().set("bbb", "111");
        redisTemplate.opsForValue().set("name", "Tom");
        System.out.println(redisTemplate.opsForValue().get("name"));
        redisTemplate.expire("a", 1, TimeUnit.SECONDS);
        //Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));

    }

    @Test
    public void testPublishMessage() throws Exception {
        String msg = "Hello, Redis!";
        if(redisDAO == null){
            System.out.println("redisDAO为空");
        }
        //redisDAO.sendMessage("java", msg); //发布字符串消息
        //redisDAO.sendMessage("php", msg); //发布字符串消息
        redisDAO.test();


//        RedisTestBean bean = new RedisTestBean();
//        bean.setName("Redis");
//        bean.setOld(2);
//        redisDAO.sendMessage("java", bean); //发布一个普通的javabean消息
//
//
//        Integer[] values = new Integer[]{21341,123123,12323};
//        redisDAO.sendMessage("java", values);  //发布一个数组消息
    }

    @Test
    public void testSpringCache(){
        for(int i = 0;i < 5;i++){
            String value = testCacheable();
            System.out.println("value:"+value);
        }

    }

    @Cacheable("testCacheable")
    public String testCacheable(){
        System.out.println("testCacheable");
        return "秋叶原";
    }

    @Test
    public void testString(){
        String.valueOf(null);
    }

}
